package socketVersion.Backend.Customer;

import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Vector;

import socketVersion.Reply;
import socketVersion.GenericServer;

public class CustomerServer extends GenericServer{

	private CustomerServerImplementation customerServer;
	
	public CustomerServer(int port, String carIp, String flightIp, String roomIp) throws IOException {
		super(port);
		customerServer = new CustomerServerImplementation(carIp,flightIp,roomIp);
	}

	@Override
	protected Reply handleMethod(String methodName, LinkedList<Object> args) {
		LinkedList<Object> returnValues;
		Reply reply = null;
		//In the event that parameters are casted into types that are not the intended type
		try{
			if("newCustomer".equals(methodName)&&(args.size()==1)){
				int id = ((Integer)args.getFirst()).intValue();
				int result;
				synchronized(customerServer){
					result = customerServer.newCustomer(id);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			if("newCustomer".equals(methodName)&&(args.size()==2)){
				int id = ((Integer)args.get(0)).intValue();
				int cid = ((Integer)args.get(1)).intValue();
				boolean result; 
				synchronized(customerServer){
					result = customerServer.newCustomer(id,cid);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			if("deleteCustomer".equals(methodName)&&(args.size()==2)){
				int id = ((Integer)args.get(0)).intValue();
				int customer = ((Integer)args.get(1)).intValue();
				boolean result;
				synchronized(customerServer){
					result = customerServer.deleteCustomer(id, customer);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			if(("queryCustomerInfo".equals(methodName))&&(args.size()==2)){
				int id = ((Integer)args.get(0)).intValue();
				int customer = ((Integer)args.get(1)).intValue();
				String result;
				synchronized(customerServer){
					result = customerServer.queryCustomerInfo(id, customer);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			if("itinerary".equals(methodName)&&(args.size()==6)){
				int id = ((Integer)args.get(0)).intValue();
				int customer = ((Integer)args.get(1)).intValue();
				Vector flightNumbers = (Vector)args.get(2);
				String location = (String)args.get(3);
				boolean car = ((Boolean)args.get(4)).booleanValue();
				boolean room = ((Boolean)args.get(5)).booleanValue();
				boolean result = customerServer.itinerary(id,customer,flightNumbers,location,car,room);
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			//None worked
			
			if(reply!=null){
				return reply;
			}
			else{
				return new Reply(Reply.STATUS_METHOD_NOT_FOUND,new LinkedList<Object>());
			}
			
		} catch(Exception e){
			e.printStackTrace();
			return new Reply(Reply.STATUS_BAD_REQUEST,new LinkedList<Object>());
		}

	}
	
	public static void main(String[] args){
		
		if(args.length!=4){
			System.out.println("Usage: java CustomerServer customerServerPort carServerIP:port roomServerIP:port flightServerIp:port");
			System.exit(0);
		}
		
		int port=0;
		
		try{
			port = Integer.parseInt(args[0]);
		} catch(NumberFormatException e){
			System.out.println("Usage: java CustomerServer customerServerPort carServerIP:port roomServerIP:port flightServerIp:port");
			System.exit(0);
		}
		
		String carIp = args[1];
		String roomIp = args[2];
		String flightIp = args[3];
		
		
		
		CustomerServer server;
		try {
			server = new CustomerServer(port,carIp,flightIp,roomIp);
			server.start();
		} catch (IOException e) {
			System.err.println("Server was unable to start");
			e.printStackTrace();
		}
	}
	
}
